var KOA 		= KOA || {};

	
KOA.Prefs = new function() {
	this.debug 		= true;
	this.animating	= false;
};


KOA.Debug = function(message) {

	if (typeof console != "object") return false;
	if (KOA.Prefs.debug !== true) return false;
	
	console.log(message);

};



KOA.Utils = new function() {


	this.setExplicitSize = function(element) {
		element.css("height", element.height());
		element.css("width", element.width());
	};


	this.initDefaultInput = function(region) {
	
	    var region = region || $('BODY');
	
		region.find('INPUT[default],TEXTAREA[default]').each(function() {		
		
			var field 	= $(this);
			var def 	= field.attr('default');
			
			$(this).focus(function() {
				if (field.val() == def) {
					field.removeClass('default');
					field.val('');
				}
			});
				
			$(this).blur(function() {
				if (!field.val() || (field.val() == def)) {
					field.addClass('default');
					field.val(def);
				}
			}).trigger('blur');
		
		});
	
	};


	this.preload = function(images, options) {
	
		var onComplete;
		var onEach;

		var image_attr  = options.attr || 'src';
		var delay       = options.delay || false;
		var type        = options.type || 'img';
		
		// $(images).map(function() { return this.src; }).get()
		
		if (options && (typeof options =='object')) {
			onComplete 	= options.onComplete || jQuery.noop();
			onEach 		= options.onEach || jQuery.noop();
		} else {
			// Support for old preload method which only had onComplete
			onComplete 	= options || jQuery.noop();
		}
	
		// Call this non-linearly
		setTimeout(function() {

			var total			= images.length;			
			var loaded_count	= 0;
			var loaded_src 		= [];
			
			var done	= function(img) {

				loaded_count++;
				
				// Callback for each image being loaded
				if ($.isFunction(onEach)) onEach(img);  
				
				// Callback for entire set of images being loaded
				if (loaded_count === total && $.isFunction(onComplete)) {
				    
				    if (delay) {
				        setTimeout(function() {onComplete(images); }, delay);				    
				    } else {
					    onComplete(images);  
					}
				}			
			};
			
			if (images.length) {

				images.each(function() {
					
					var img = $(this);
					var src = '';
					
					if (type == 'bg') {
					    src = img.css('background-image');
					    src = src.replace(/"/g,"").replace(/url\(|\)$/ig, "")
					} else {
    					src = img.attr(image_attr);
					}					
					
					// No source found, let's just fire done event
					if (!src.length && src != 'none') done(img);
					
					// Make sure we have not already loaded this image
					if (jQuery.inArray(src, loaded_src) > 0) done(img);
					
					// Create new image object, this gets around problems with browser cache
					var load_img = new Image();
					$(load_img).bind('load error', function(e) {
						done(img, load_img);
						$.data(this, 'loaded', ('error'==e.type)?false:true);
					});
					$(load_img).attr('src', src);

				});
	
			} else {
				
				// No images to load, let's get outta here
				if ($.isFunction(callback)) onComplete(images);  		
	
			}
		
		}, 0);

	};

	this._preload = function(images, options) {
	
		var onComplete;
		var onEach;

		var image_attr  = options.attr || 'src';
		var delay       = options.delay || false;
		
		// $(images).map(function() { return this.src; }).get()
		
		if (options && (typeof options =='object')) {
			onComplete 	= options.onComplete || jQuery.noop();
			onEach 		= options.onEach || jQuery.noop();
		} else {
			// Support for old preload method which only had onComplete
			onComplete 	= options || jQuery.noop();
		}
	
		// Call this non-linearly
		setTimeout(function() {

			var total			= images.length;			
			var loaded_count	= 0;
			var loaded_src 		= [];
			
			var done	= function(img) {

				loaded_count++;
				
				// Callback for each image being loaded
				if ($.isFunction(onEach)) onEach(img);  
				
				// Callback for entire set of images being loaded
				if (loaded_count === total && $.isFunction(onComplete)) {
				    
				    if (delay) {
				        setTimeout(function() {onComplete(images); }, delay);				    
				    } else {
					    onComplete(images);  
					}
				}			
			};
			
			if (images.length) {

				images.each(function() {
					
					var img = $(this);
					var src = img.attr(image_attr);
					
					// No source found, let's just fire done event
					if (!src.length) done(img);
					
					// Make sure we have not already loaded this image
					if (jQuery.inArray(src, loaded_src) > 0) done(img);
					
					// Create new image object, this gets around problems with browser cache
					var load_img = new Image();
					$(load_img).bind('load error', function(e) {
						done(img, load_img);
						$.data(this, 'loaded', ('error'==e.type)?false:true);
					});
					$(load_img).attr('src', src);

				});
	
			} else {
				
				// No images to load, let's get outta here
				if ($.isFunction(callback)) onComplete(images);  		
	
			}
		
		}, 0);

	};

};


KOA.Init = new function() {

	$(document).ready(function() {

		KOA.$Body 	= $('BODY');
		window.c 	= KOA.Debug;
		
		// Utilities to auto run
		KOA.Utils.initDefaultInput();
		
		// Execute all element classes		
		$('[data-script]').each(function(){
			
			var that    = this;	
			var classes = $(this).data("script").split(" ");
			
			$.each(classes, function(i, v){				
				KOA.Invoke(that, v);
			});

		});
	
	});

};


KOA.Invoke = function(element, command, options) {
	if (command.length && KOA.hasOwnProperty(command)) {
		(KOA[command]).call($(element), options);
	}
};


